package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseStorageOrder;
import com.wing.product.model.response.WarehouseOutboundOrderVO;
import com.wing.product.model.response.WarehouseOutboundRecordVO;
import com.wing.product.service.WarehouseOutboundRecordService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "出库记录管理", tags = "#/warehouse/product/warehouseOutboundRecord")
@RestController("WarehouseOutboundRecordController")
@RequestMapping("/warehouse/outboundRecord")
@RequiredArgsConstructor
public class WarehouseOutboundRecordController {

    private final WarehouseOutboundRecordService warehouseOutboundRecordService;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseOutboundRecordVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String warehouseOutboundRecordCode,
            Long warehouseOutboundOrderId,
            Long customerId
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseStorageOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("warehouseOutboundRecordCode", warehouseOutboundRecordCode);
        paramMap.put("customerId", customerId);
        paramMap.put("warehouseOutboundOrderId", warehouseOutboundOrderId);

        //搜索
        IPage<WarehouseOutboundRecordVO> list = warehouseOutboundRecordService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询出库记录明细", notes = "查询")
    @GetMapping ("/selectRecordDetail")
    public JsonResult<WarehouseOutboundRecordVO> selectProduct(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseOutboundRecordVO warehouseOutboundRecordVO = warehouseOutboundRecordService.selectRecordDetail(id);
            if (ObjectUtil.isNotEmpty(warehouseOutboundRecordVO)){
                return  JsonResult.success("请求成功",warehouseOutboundRecordVO);
            }else {
                return  JsonResult.success("数据为空",warehouseOutboundRecordVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "添加出库记录", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseOutboundRecordVO warehouseOutboundRecordVO)  {
        try{
            return warehouseOutboundRecordService.saveWarehouseOutboundRecord(warehouseOutboundRecordVO);
        }catch (Exception e){
//            System.out.println(e);
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());


        }
    }

    @ApiOperation(value = "删除出库记录", notes = "删除")
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long id) {
        return warehouseOutboundRecordService.deleteWarehouseOutboundRecord(id);
    }

}
